﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NVelocity;
using NVelocity.App;

namespace HCDV.HCString
{
    public  class Stringexe
    {
        /// <summary>
        ///执行VE得出结果
        /// </summary>
        /// <param name="sin">VE文本</param>
        /// <param name="paras_name">参数名称</param>
        /// <param name="paras">参数值(string num  list ===)</param>
        /// <returns></returns>
        public string hcvlForString(string sin, string[] paras_name, object[] paras)
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            //builder.Append("#foreach($u in $ListUsers)\r\n" +
            //    "#beforeall\r\n" +
            //    "<table border=\"0\" cellpadding=\"10\" cellspacing=\"10\">" +
            //    "<tr><td>Name</td><td>Sex</td><td>City</td></tr>" +
            //    "#each\r\n" +
            //    "<tr>" +
            //    "<td>$u.Name</td>" +
            //    "<td>$u.Sex</td>" +
            //    "<td>$u.City</td>" +
            //    "</tr>" +
            //    "#afterall\r\n" +
            //    "</table>" +
            //    "#nodata\r\n" +
            //    "暂无用户资料\r\n" +
            //    "#end");

            builder.Append(sin);

            VelocityEngine vltEngine = new VelocityEngine();
            vltEngine.Init();

            VelocityContext vltContext = new VelocityContext();
            for (int i = 0; i < paras_name.Length; i++)
            { vltContext.Put(paras_name[i], paras[i]); }
           
            //vltContext.Put("ListUsers", listUsers);

            System.IO.StringWriter vltWriter = new System.IO.StringWriter();
            vltEngine.Evaluate(vltContext, vltWriter, null, builder.ToString());
            string re = (vltWriter.GetStringBuilder().ToString());
            return re;
        }

    }
}
